
Page({

  data: {
    id:'',
    phone:'',
    name:'',
    studyNumber:'',
    activity:{}
  },

  onLoad: function (options) {
    console.log(options);
    const that = this;
    this.setData({
      id:options.id
    });

    // 获取活动数据
    wx.cloud.callFunction({
      name:'downDetail',
      data:{
        id:options.id
      },
      success:res=>{
        console.log('=========',res);
        that.setData({
          activity:res.result.data
        })
      }
    })
  },

  // 提交
  submit(e){
    console.log("提交",e);
    const {name,phone,studyNumber} = e.detail.value;
    const regexName = /^[\u4e00-\u9fa5]{2,5}$/; // 匹配中文名字
    const regexName2 = /^[a-z]{3,20}$/i;  //匹配英文名字
    const regexPhone = /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/; //匹配手机号码
    const regexNumber = /^[0-9]{10}$/;  // 匹配10位数字学号

    const {surfacePlot,title,provinces,detailAddress,startTime}  = this.data.activity;
    if((regexName.test(name)||regexName2.test(name))&&regexPhone.test(phone)&&regexNumber.test(studyNumber)){
      // 当全部是有效值 才插入到数据库中
      const info =  {
        name,
        phone,
        studyNumber,
        activeId:this.data.id,
        surfacePlot,
        title,
        provinces,
        detailAddress,
        startTime
      };

      const userInfo = wx.getStorageSync('userInfo');
        
      const userInfoObj = {
        nickName:userInfo.nickName,
        avatarUrl:userInfo.avatarUrl
      }


      // 提交的话 显示提交中字样
      wx.showLoading({
        title: '报名中',
        mask: true,
      });

      wx.cloud.callFunction({
        name:'upJoin',
        data:{
          info,
          userInfoObj
        },
        success:res=>{
          console.log("报名成功",res)
          wx.switchTab({
            url: '/pages/mine/mine',
          });
          wx.hideLoading();
        },
        fail:err=>{
          console.log('提交失败',err);
          wx.showModal({
            title: '提示',
            content: `出错啦${err}`,
            showCancel: false,
            confirmText: '知道啦',
            confirmColor: '#3CC51F',
          });
        }
      }); 
    }else{
      // 如果不是的话 则弹框弹框提示
      wx.showModal({
        title: '提示',
        content: '请正确填写信息。名字为2-5位中文字符或3-20位英文字符，手机号为11位字符，学号为10位字符',
        showCancel: false,
        confirmText: '知道了',
        confirmColor: '#3CC51F',
      });
        
    }
  },
})